//
//  IntroLayer.m
//  CutFruit
//
//  Created by BulletHermit on 13-2-1.
//  Copyright __MyCompanyName__ 2013年. All rights reserved.
//


// Import the interfaces
#import "IntroLayer.h"

#import "CCBReader.h"

#pragma mark - IntroLayer

// HelloWorldLayer implementation
@implementation IntroLayer

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	IntroLayer *layer = [IntroLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

//
-(id) init
{
	if( (self=[super init])) {
		
		// ask director for the window size
		CGSize size = [[CCDirector sharedDirector] winSize];
		
		CCSprite *background;
		
		if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
			background = [CCSprite spriteWithFile:@"Default.png"];
			background.rotation = 90;
		} else {
			background = [CCSprite spriteWithFile:@"Default-Landscape~ipad.png"];
		}
		background.position = ccp(size.width/2, size.height/2);
		
		// add the label as a child to this Layer
		[self addChild: background];
        [self initData];
	}
	
	return self;
}
-(void) initData
{
    
    AppController *appDelegate =(AppController*) [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription
                                                  entityForName:@"Volume"
                                                  inManagedObjectContext:context];
    [request setEntity:entityDescription];

        
    NSArray *objects = [context executeFetchRequest:request
                                                  error:&error];
        
    if (objects == nil) {
        NSLog(@"There was an error!");
            // Do whatever error handling is appropriate
    }
    if ([objects count] > 0)
        return;
    else
    {
        NSManagedObject *theVolumeSound = [NSEntityDescription
                       insertNewObjectForEntityForName:@"Volume"
                       inManagedObjectContext:context];
        
        [theVolumeSound setValue:@"sound" forKey:@"kind"];
        [theVolumeSound setValue:[NSNumber numberWithFloat:0.5] forKey:@"value"];

        NSManagedObject *theVolumeMusic = [NSEntityDescription
                                           insertNewObjectForEntityForName:@"Volume"
                                           inManagedObjectContext:context];
        
        [theVolumeMusic setValue:@"music" forKey:@"kind"];
        [theVolumeMusic setValue:[NSNumber numberWithFloat:0.8] forKey:@"value"];
    }
    
    
    [context save:&error];
}
-(void) onEnter
{
	[super onEnter];
    CCScene* welcomeUI = [CCBReader sceneWithNodeGraphFromFile:@"WelcomeUI.ccbi"];
    CCLOG(@"ss");
	[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:welcomeUI ]];
}
@end
